<!--
 * @描述: 组织机构的公共组件
 * @版权所有: 广东国星科技有限公司
 * @商业授权: www.mscodecloud.com
-->
<template>
  <div class="app-container">
    <el-container style="width: 100%">
      <div ref="asideDiv" style="width: 150px; overflow: scroll; margin-right: 5px; border: 1px solid #C0D4F0">
        <el-aside style="background-color: white; padding: 0px 5px 0px 0px">
          <div style="width: 140px; padding: 10px 10px 0px 10px">
            <el-input v-model="orgName" :placeholder="$t('sysorg.orgName')" clearable />
          </div>
          <el-table
            row-key="id"
            :data="allOrgList"
            fit
            highlight-current-row
            style="width: 100%; cursor: pointer"
            class="aside-table"
            @row-click="clickAsideData"
          >
            <el-table-column :label="$t('sysorg.orgName')" prop="orgName" />
          </el-table>
        </el-aside>
      </div>
      <div style="width: 8px; margin-right: 5px; background-color: #C0D4F0">
        <i v-if="leftIcon" class="el-icon-caret-left" style="position: relative; top: 50%; left: -3px; cursor: pointer; color: #97A8BE" @click="handleLeftIcon" />
        <i v-if="rightIcon" class="el-icon-caret-right" style="position: relative; top: 50%; left: -5px; cursor: pointer; color: #97A8BE" @click="handleRightIcon" />
      </div>
      <div ref="mainDiv" style="width: 87%">
        <div class="filter-container">
          <el-input v-model="listQuery.orgName" :placeholder="$t('sysorg.orgName')" clearable class="filter-input filter-item" style="width: 160px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.orgDescription" :placeholder="$t('sysorg.orgDescription')" clearable class="filter-input filter-item" style="width: 160px" @keyup.enter.native="handleFilter" />
          <el-select v-model="listQuery.orgType" :placeholder="$t('sysorg.orgType')" clearable class="filter-input filter-item" style="width: 120px">
            <el-option v-for="item in orgTypeOptions" :key="item.value" :label="item.name" :value="item.value" />
          </el-select>
          <el-button v-waves type="primary" class="filter-item" icon="el-icon-search" @click="handleFilter">
            {{ $t('common.query') }}
          </el-button>
          <el-button v-waves type="primary" class="filter-item" icon="el-icon-refresh-right" @click="handleReset">
            {{ $t('common.reset') }}
          </el-button>
        </div>
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          row-key="id"
          :data="list.slice((listQuery.currentPage-1)*listQuery.pageSize,listQuery.currentPage*listQuery.pageSize)"
          border
          fit
          highlight-current-row
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="40" />
          <el-table-column :label="$t('sysorg.orgName')" prop="orgName" />
          <el-table-column :label="$t('sysorg.orgType')" prop="orgTypeCn" />
          <el-table-column :label="$t('sysorg.orgDescription')" prop="orgDescription" />
          <el-table-column :label="$t('sysorg.orgSequence')" prop="orgSequence" />
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>
    </el-container>
    <div slot="footer" class="dialog-footer" style="float: right">
      <el-button @click="hideSysOrgComponent()">
        {{ $t('common.cancel') }}
      </el-button>
      <el-button type="primary" @click="createSysOrgComponentMsg()">
        {{ $t('common.confirm') }}
      </el-button>
    </div>
  </div>
</template>

<script>
import { querySysOrg, queryOrgType } from '@/api/admin'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'SysOrg',
  components: { Pagination },
  directives: { waves },
  props: ['msg'], // eslint-disable-line
  data() {
    return {
      leftIcon: true,
      rightIcon: false,
      labelPosition: 'right',
      list: [],
      allOrgList: this.$store.getters.orgs,
      total: 0,
      listLoading: true,
      orgName: undefined,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        orgName: undefined,
        orgType: undefined,
        orgDescription: undefined,
        id: undefined,
        parentOrgName: undefined
      },
      multipleSelection: [],
      orgTypeOptions: [],
      parentName: '',
      temp: {
        id: undefined
      }
    }
  },
  watch: {
    msg(oldValue, newValue) { },
    orgName(value) {
      if (!value) {
        this.allOrgList = this.$store.getters.orgs
      } else {
        querySysOrg({ 'orgName': value }).then(response => {
          this.allOrgList = response.data.list
        })
      }
    }
  },
  created() {
    if (window.innerWidth < 700) {
      this.labelPosition = 'top'
    }
    this.getList()
  },
  methods: {
    handleLeftIcon() {
      this.$refs.asideDiv.style.width = '10px'
      this.$refs.asideDiv.style.minWidth = '10px'
      this.$refs.mainDiv.style.width = '98%'
      this.leftIcon = false
      this.rightIcon = true
    },
    handleRightIcon() {
      this.$refs.asideDiv.style.width = '140px'
      this.$refs.asideDiv.style.minWidth = '120px'
      this.$refs.mainDiv.style.width = '80%'
      this.leftIcon = true
      this.rightIcon = false
    },
    getList() {
      this.listLoading = true
      querySysOrg(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.pagination.total
        this.listLoading = false
      })
      queryOrgType().then(response => {
        this.orgTypeOptions = response.data.list
      })
    },
    clickAsideData(row, event, column) {
      this.listQuery.id = row.id
      this.listQuery.parentOrgName = row.orgName
      this.listLoading = true
      querySysOrg(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.pagination.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.currentPage = 1
      this.listQuery.id = undefined
      this.listQuery.parentOrgName = undefined
      this.getList()
    },
    handleReset() {
      this.listQuery.orgName = undefined
      this.listQuery.orgType = undefined
      this.listQuery.orgDescription = undefined
      this.listQuery.id = undefined
      this.listQuery.parentOrgName = undefined
      this.handleFilter()
    },
    handleSelectionChange(val) {
      if (this.msg === 'single') {
        if (val.length > 1) {
          this.$refs.multipleTable.clearSelection()
          this.$refs.multipleTable.toggleRowSelection(val.pop())
        } else {
          this.multipleSelection = val
        }
      } else {
        this.multipleSelection = val
      }
    },
    hideSysOrgComponent() {
      this.$emit('hideSysOrgComponent', 1)
    },
    createSysOrgComponentMsg() {
      this.$emit('sysOrgComponentMsg', this.multipleSelection)
      this.$emit('hideSysOrgComponent', 1)
    },
    handleWarning(response) {
      this.$message({
        message: response.message || response,
        type: 'warning',
        duration: 2000
      })
    }
  }
}
</script>
<style>
  .aside-table td,.aside-table th.is-leaf {
    border: none;
  }
  .aside-table::before {
    width: 0;
  }
</style>
<style scoped>
  .el-table .cell {
    white-space: nowrap;
    text-overflow: unset;
    margin-right: 10px;
  }
  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }
  ::-webkit-scrollbar-thumb {
    background-color: #C0D4F0;
    border-radius: 3px;
  }
  .pagination-container[data-v-72233bcd] {
    padding: 0
  }
</style>
